﻿// <copyright file="FileSelectionUC.cs" company="The University of Scranton"> 
// Copyright (c) 2010 All Rights Reserved, http://sdpmt.codeplex.com 
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY  
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A 
// PARTICULAR PURPOSE. 
// 
// </copyright> 
// <author>Luke Benedict</author> 
// <email>luke.benedict@ieee.org</email> 
// <date>2010-11-04</date> 
// <summary>Contains a class that allows for file selection</summary>

namespace Sdpmt.Coreprocessing
{
    #region Namespace Usage

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Sdpmt.Filereader;

    #endregion
    
    /// <summary>
    /// A class that allows for file selection
    /// </summary>
    public partial class FileSelectionUC : UserControl
    {
        #region Private Members

        /// <summary>
        /// The path to the file that is being selected by this UserControl
        /// </summary>
        private string filePath;

        /// <summary>
        /// The arguments associated with the file selection event
        /// </summary>
        private FileSelectionEventArgs args;

        /// <summary>
        /// The list of defects that is being populated by this file read
        /// </summary>
        private System.Collections.Generic.SortedDictionary<DateTime, double> sortedDictionary;

        #endregion

        #region Constructor

        public FileSelectionUC()
        {
            InitializeComponent();
            this.args = new FileSelectionEventArgs();
        }

        #endregion

        #region Public Delegates

        public delegate void MyDelegate(object sender, FileSelectionEventArgs e);   // delegate declaration

        #endregion

        #region Public Events

        public event MyDelegate MyEvent;

        #endregion

        #region Internal Properties

        internal System.Collections.Generic.SortedDictionary<DateTime, double> SortedDictionary
        {
            set { this.sortedDictionary = new SortedDictionary<DateTime, double>(value); }
        }

        #endregion        

        #region Private Methods

        /// <summary>
        /// Handles when the "Browse" button is clicked by the user
        /// </summary>
        /// <param name="sender">Sender, the browse button</param>
        /// <param name="e">Event args</param>
        private void BrowseButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog f = new OpenFileDialog();
            f.Filter = "CSV File|*.csv";
            f.ShowDialog();
            this.filePath = f.FileName;

            this.filePathTextBox.Text = this.filePath;     
        }

        /// <summary>
        /// Handles when the "Load File" button is clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoadFileButton_Click(object sender, EventArgs e)
        {
            // Make sure that the end date is after the start date
            if (this.startDateTimePicker.Value.Date >= this.endDateTimePicker.Value.Date)
            {
                MessageBox.Show("End Date must be greater than begin date!");
                return;
            }

            this.statusLabel.Hide();

            // Read the file
            this.backgroundWorker.RunWorkerAsync();           
        }

        /// <summary>
        /// Handles when the FileSelectionUC is loaded
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FileSelectionUC_Load(object sender, EventArgs e)
        {
            this.startDateTimePicker.Value = this.endDateTimePicker.Value.AddYears(-1);
        }        

        /// <summary>
        /// Handles when the BackgroundWorker is told to do work
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker bw = sender as BackgroundWorker;
            
            this.sortedDictionary = CsvReader.Read(this.filePath, this.startDateTimePicker.Value.Date, this.endDateTimePicker.Value.Date, bw);
        }

        /// <summary>
        /// Handles when the BackgroundWorker has finished doing its work
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.args.Data = new SortedDictionary<DateTime, double>(this.sortedDictionary);
            this.MyEvent(this, this.args);
            this.statusLabel.Show();
            this.statusLabel.Text = "File Load Complete";
        }

        /// <summary>
        /// Handles when progress has been reported by the BackgroundWorker
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.progressBar.Value = e.ProgressPercentage;
        }

        #endregion
    } // end class    
} // end namespace
